package de.tapps.brainstorming;


import java.nio.ByteBuffer;
import java.security.spec.*;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.RandomAccessFile;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.Security;
import org.spongycastle.jce.provider.JCERSAPrivateCrtKey;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import org.spongycastle.jce.provider.BouncyCastleProvider;


//import android.util.Base64;
import android.util.Log;
import org.bouncycastle.jce.provider.JCERSAPrivateKey;
public class Crypter {
	Key publicKey;
	Key privateKey;
	
	static {
		Security.insertProviderAt(new org.spongycastle.jce.provider.BouncyCastleProvider(), 1);
	}

	private MainActivity activity = new MainActivity();

	public Crypter(MainActivity act) {
		activity = act;
	}

	private PrivateKey getPrivateKey()
	  {
		  
			    Security.addProvider(new org.spongycastle.jce.provider.BouncyCastleProvider());
			    PrivateKey privateKey = null;
			    try {
				InputStream pris = activity.getResources().openRawResource(R.raw.privatekey);
		        BufferedInputStream bpris = new BufferedInputStream(pris);

				byte[] encodedPrivateKey = null;
				try {
					encodedPrivateKey = new byte[bpris.available()];
				
				bpris.read(encodedPrivateKey);
				bpris.close();
				}
				catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				// Generate KeyPair.
				KeyFactory keyFactory;		
						keyFactory = KeyFactory.getInstance("RSA");
					    PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(
								encodedPrivateKey);
							privateKey = keyFactory.generatePrivate(privateKeySpec);
						}
						catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
				return privateKey;
	  }

	  private Key getPublicKey()
	  {
		    Security.addProvider(new org.spongycastle.jce.provider.BouncyCastleProvider());
		    InputStream is = activity.getResources().openRawResource(R.raw.publickey);
	        BufferedInputStream bis = new BufferedInputStream(is);
	        PublicKey publicKey = null;
	        try {
			byte[] encodedPublicKey = new byte[bis.available()];

			
				bis.read(encodedPublicKey);
			
			bis.close();
	 
	 
			// Generate KeyPair.
			KeyFactory keyFactory;
				keyFactory = KeyFactory.getInstance("RSA");

			X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(
					encodedPublicKey);
			
				publicKey = keyFactory.generatePublic(publicKeySpec);
	        }
			catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			catch (InvalidKeySpecException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			catch (NoSuchAlgorithmException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return publicKey;

	  }
	public String encrypt(String paramString)
	  {
	    Cipher cipher;
	    byte[] cipherData = null;
		try {
			cipher = Cipher.getInstance("RSA");
		
	    cipher.init(Cipher.ENCRYPT_MODE, getPublicKey());
	   cipherData  = cipher.doFinal(paramString.getBytes());
		}
		catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (NoSuchPaddingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (InvalidKeyException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (IllegalBlockSizeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (BadPaddingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return Base64.encodeToString(cipherData,Base64.NO_WRAP);
	  }
	
	public String decrypt(String encryptedstring)
	  {
		byte[] encrypted = Base64.decode(encryptedstring, Base64.NO_WRAP);
	    Cipher cipher;
	    byte[] cipherData = null;
		try {
			cipher = Cipher.getInstance("RSA");
		try {
			cipher.init(Cipher.DECRYPT_MODE, getPrivateKey());
		}
		catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		  cipherData = cipher.doFinal(encrypted);
		}
		catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (NoSuchPaddingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		catch (IllegalBlockSizeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (BadPaddingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		    return new String(cipherData);
	  
	  }

public String splitGetParameter(String s){
	int laenge = s.length();
	double bloecke;
	if (laenge%512 == 0){
		bloecke = Math.ceil(laenge/512);
	}
	else {
		
		bloecke = Math.ceil(laenge/512)+1;
	}
	int blockAnzahl = new Double(bloecke).intValue();
	String urlGetParameter = "";
	for (int i=0;i<blockAnzahl;i++){
		if ((i+1)*512 > laenge){
if (i==0){
	urlGetParameter = urlGetParameter+i+"="+s.substring(i*512,laenge);

			}
			else {
				urlGetParameter = urlGetParameter+"&"+i+"="+s.substring(i*512,laenge);
	
			}
		}
		else {
			if (i==0){
				urlGetParameter = urlGetParameter+i+"="+s.substring(i*512,(i+1)*512);

			}
			else {
				urlGetParameter = urlGetParameter+"&"+i+"="+s.substring(i*512,(i+1)*512);

			}

		}
	}
return urlGetParameter;

}
	
	
	
	

	

	
	
}
